package cn.tedu.review;

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr={9,8,7,6,5,1,2,3,4,5};
        System.out.println("bubbleSort(arr)+"+bubbleSort(arr));
        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));
    }
    public static int bubbleSort(int[] arr){
        int num=0;
        int flag=0;
        for (int i = 1; i <= arr.length-1; i++) {//外层循环length-1次
            for (int j = 0; j <=arr.length-i-1 ; j++) {//内层循环 length-i次
                num++;
                if (arr[j]>arr[j+1]){
                    int temp;
                    temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                    flag=1;
                }else{
                    flag=0;
                }
            }

            if (flag==0){
                break;
            }
        }
        return num;
    }
}

